package model;

import database.Database;
import java.sql.SQLException;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.http.HttpServletRequest;
import session.Session;
import user.User;

/**
 * Register class implements the registration model for a new user of the Lithub
 * website.
 *
 * @author Tyler Wood.
 * @version 1.0
 *
 */
public class Register extends Model {

    public Boolean userRegistered;
    public Boolean userLoggedIn;
    public String username;
    public String password;
    public String password2;
    public String email;
    private HttpServletRequest request;

    /**
     * Register() is the reciever of a registration page request from a user on
     * the lithub website to be processed.
     *
     * @param HttpServletRequest request which holds the username and password
     * information of the user who wants to sign in.
     *
     */
    public Register(HttpServletRequest req) {
        userRegistered = false; // Default to false
        userLoggedIn = false; // Default to false
        username = req.getParameter("username");
        password = req.getParameter("password");
        password2 = req.getParameter("password2");
        email = req.getParameter("email");
        request = req;
    }

    /**
     * invoke() is a validity check type invokation method, which is used when a
     * user wants to register on the Lithub website.
     *
     * @param String action which needs to correspond to the "register" action in
     * order to register a new user.
     *
     */
    @Override
    public void invoke(String action) {
        if (action.equals("register")) {
            //Try registering the user.
            userRegistered = tryRegistering(username, password, password2, email);
            if (userRegistered) //If they successfully register, we created a session for them; they're logged in.
            {
                userLoggedIn = true;
            }
        }
    }

    /**
     * tryRegistering implements the registration of a new user of the Lithub
     * website. A user must provide a username, password, re-typed password for
     * confirmation, and email to register with the Lithub database and website.
     *
     * @param username of a user whom wants to register an account.
     *
     * @param password of a user whom wants to register an account.
     *
     * @param password validity check of a user whom wants to register an
     * account.
     *
     * @param d email of a user whom wants to register an account.
     *
     * @return boolean true if the user was successfully registered and a new
     * browing session has been created for them.
     */
    public Boolean tryRegistering(String username, String password, String password2, String email) {

        //User hasn't supplied any/correct credential information. Abort.
        if (username == null || password == null || (!password.equals(password2))) {
            return false;
        }

        // Now call the Database function to see if the user exists
        Database d = Database.getDatabase();
        User u = null;
        try {
            u = d.getUserByUsernameAndPassword(username, password);
        } catch (SQLException ex) {
            Logger.getLogger(LoginPage.class.getName()).log(Level.SEVERE, null, ex);
        }

        //Check if user is not registered. 
        if (u == null || u.id == -1) {   //Register the user.
            u = new User(-1, username, password, email);
            d.addAccountToDatabase(u);
        }

        //Start a new session for the user so we can redirect them to the user page.
        Session s = new Session(request);
        s.startSession(u);

        return true;
    }
}